﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;
using TCPServer;
using Communication;
using System.Net;
using System.Threading;

namespace ServerConsoleTest
{
    class Program
    {
        static private Server svr;

        static void Main(string[] args)
        {
            svr = new Server(new IPEndPoint(IPAddress.Any, 3000));
            //svr = new Server(new IPEndPoint(IPAddress.Parse("5.48.197.212"), 3000));
            Console.WriteLine("Server started up.");
            Console.WriteLine("My IP is " + IPUtil.GetLocalIP().ToString());

            while (true)
            {
                string message;
                message = Console.ReadLine();

                bool contain = false;
                Commands.checkWithParam(message, CommandsSent.paramCommands, out contain);
                Commands.checkFor(message, CommandsSent.commands, out contain);

                //message type : username#message
                int sharpIndex = message.IndexOf("#");
                if (sharpIndex > 0)
                {
                    string usr = message.Substring(0, sharpIndex);
                    string usrMessage = message.Substring(sharpIndex + 1, message.Length - sharpIndex - 1);
                    if (usr.Equals("all"))
                    {
                        svr.SendToAllClients(usrMessage);
                    }
                    else
                    {
                        svr.Send(usr, usrMessage);
                    }
                }
            }

        }

        static internal void sendAll(string message)
        {
            //svr.MessageAll(message);
        }

        static internal void numberOfClients()
        {
            Console.WriteLine(svr.GetNumebrOfClients());
        }

    }
}
